<%--

  User: PING
  Date: 2022/9/7
  Time: 9:50
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"
        import="com.javaweb.common.DBUtil"
        import="com.javaweb.common.StringTool"
        import="com.javaweb.common.DispatcherTool"%>
<%@ page import="java.util.Date" %>
<%@ page import="com.javaweb.common.ServiceResponse" %>
<%@ page import="java.util.concurrent.ConcurrentHashMap" %>
<%@ page import="java.util.Map" %>

<html>
<meta charset="UTF-8">
<head>
    <title></title>
</head>
<body>
    <%
        // process the request from login UI
        String tId = request.getParameter("tId") ;
        String uId = request.getParameter("uId") ;
        if (StringTool.isEmpty(tId) || StringTool.isEmpty(uId)) {
            response.sendRedirect("show.jsp") ;
            return ;
        }
        String msg = null ;
        if (session.getAttribute("voted") != null ) {
            msg = StringTool.URLEncodeInUTF("您刚才已经投过了！") ;

        } else {
            ServiceResponse result = DBUtil.doVote(StringTool.parseToInt(tId), StringTool.parseToInt(uId), new Date());

            if (result.isSucceed()) {
                session.setAttribute("voted", true);
                Map<Integer, Integer> voteMap = (ConcurrentHashMap<Integer, Integer>)application.getAttribute("voteMap") ;
                voteMap.put(StringTool.parseToInt(tId), voteMap.get(StringTool.parseToInt(tId)) + 1) ;
            }
            msg = StringTool.URLEncodeInUTF(result.getMsg()) ;
        }
        response.sendRedirect("show.jsp?msg=" + msg);
    %>

</body>
</html>
